package com.biz;

import com.entity.Order;
import com.entity.Stock;
import com.vo.MergeVO;
import com.vo.req.MergeRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderBizImpl implements OrderBiz{

    @Override
    public MergeVO merge(MergeRequest mergeRequest) {
        List<Order> orders = mergeRequest.getOrder();
        List<Stock> stocks = mergeRequest.getStock();
        HashMap<String,Integer> map = new HashMap<>();
        for (Order order : orders) {
            map.put(order.getGoodsId(),map.getOrDefault(order.getGoodsId(),0)+order.getNumber());
        }
        for (Stock stock : stocks) {
            map.put(stock.getGoodsId(),map.getOrDefault(stock.getGoodsId(),0)+stock.getNumber());
        }
        List<Stock> responses = new ArrayList<>();
        for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
            Stock stock = new Stock();
            stock.setGoodsId(stringIntegerEntry.getKey());
            stock.setName(getName(stringIntegerEntry.getKey()));
            stock.setNumber(stringIntegerEntry.getValue());
            responses.add(stock);
        }
        MergeVO mergeVO = new MergeVO();
        mergeVO.setStock(responses);
        return mergeVO;
    }

    public String getName(String goodsId){
        String name = "";
        switch (goodsId){
            case "1001":
                name = "苹果手机";
                break;
            case  "1002":
                name = "华为手机";
                break;
            case "1003":
                name = "小米手机";
                break;
            case "1004":
                name = "天音手机";
        }
        return name;
    }
}
